const express=require('express')
const pool=require('../pool')
const router=express.Router()

// 查询所有分店
router.get('/list',(req,res,next)=>{
    pool.query('select * from branch',(err,r)=>{
        if(err){
            return next(err)
        }
        res.send({code:200,msg:'查询所有分店成功',data:r})
    })
})

// 通过id查询分店
router.get('/queryById',(req,res,next)=>{
    let obj=req.query
    let branchId=obj.branchId
    pool.query('select * from branch where branchId=?',[branchId],(err,r)=>{
        if(err){
            return next(err)
        }
        res.send({code:200,msg:'查询指定分店成功',data:r})
    })
})

// 删除分店
router.get('/delete',(req,res,next)=>{
    let obj=req.query
    let branchId=obj.branchId
    pool.query('delete from branch where branchId=?',[branchId],(err,r)=>{
        if(err){
            return next(err)
        }
        res.send({code:200,msg:'删除分店成功',data:r})
    })
})

// 增加分店
router.post('/add',(req,res,next)=>{
    let obj=req.body
    pool.query('insert into branch set ?',[obj],(err,r)=>{
        if(err){
            return next(err)
        }
        res.send({code:200,msg:'增加分店成功',data:r})

    })
})

// 修改分店
router.post('/modify',(req,res,next)=>{
    let obj=req.body
    pool.query('update branch set ? where branchId=?',[obj,obj.branchId],(err,r)=>{
        if(err){
            return next(err)
        }
        res.send({code:200,msg:'修改分店成功',data:r})
    })
})

module.exports=router

